﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;
using System.IO.Ports;
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.Sqlite;

namespace EHR18
{
    public partial class FrmSerialPortSetting : BaseDialog2
    {
        SerialConfig _cfg = null;
        SerialDevice _device = null;
        public FrmSerialPortSetting(SerialDevice device)
        {
            InitializeComponent();
            _device = device;
            this.Text = device.DeviceName;
            this.Name = device.DeviceName;
        }
        private void skinGroupBox1_Enter(object sender, EventArgs e)
        {
            
        }

        public void FrmSerialPortSetting_Load(object sender, EventArgs e)
        {
            cboCOMName.Items.AddRange(SerialPort.GetPortNames());
            cboBaud.Items.AddRange(new string[] { "1200", "2400", "4800","9600","19200","38400","57600","115200" });
            cboDatabits.Items.AddRange(new string[] { "5", "6", "7", "8" });
            cboStopbits.Items.AddRange(new string[] { "0","1", "1.5", "2" });
            cboParity.Items.AddRange(new string[] { "None", "Odd", "Even" });
            pictureBox1.BackColor = _device.IsOpen ? Color.LimeGreen : Color.Red;

            _cfg = _device.getCurrentConfig();
            cboCOMName.Text = _cfg.COM;
            cboBaud.Text = _cfg.Baud;
            cboDatabits.Text = _cfg.Databits;
            cboParity.Text = _cfg.Parity;
            cboStopbits.Text = _cfg.Stopbits;
            this.btnNo.Click += btnNo_Click;
            this.btnYes.Click+=btnYes_Click;
        }

        void btnYes_Click(object sender, EventArgs e)
        {
            _cfg.COM = cboCOMName.Text;
            _cfg.Baud = cboBaud.Text;
            _cfg.Databits = cboDatabits.Text;
            _cfg.Stopbits = cboStopbits.Text;
            _cfg.Parity = cboParity.Text;
            _device.SetConfig(_cfg);
            _device.OpenDevice();

        }

        void btnNo_Click(object sender, EventArgs e)
        {
       //     this.Close();
        }     
    }
}
